<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>商城界面</title>
  <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo layui-hide-xs layui-bg-black">呆子</div>
    <!-- 头部区域（可配合layui 已有的水平导航） -->
    <ul class="layui-nav layui-layout-left">
      <!-- 移动端显示 -->
      <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
        <i class="layui-icon layui-icon-spread-left"></i>
      </li>
      
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 1</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 2</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 3</a></li>
      <li class="layui-nav-item">
        <a href="javascript:;">nav groups</a>
        <dl class="layui-nav-child">
          <dd><a href="">menu 11</a></dd>
          <dd><a href="">menu 22</a></dd>
          <dd><a href="">menu 33</a></dd>
        </dl>
      </li>
    </ul>
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item layui-hide layui-show-md-inline-block">
        <a href="javascript:;">
          <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img">
          <span class="rootName">管理员</span>
        </a>
        <dl class="layui-nav-child">
          <dd><a href="">个人中心</a></dd>
          <dd><a href="">设置</a></dd>
          <dd><a href="" id="logout">退出</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
        <a href="javascript:;">
          <i class="layui-icon layui-icon-more-vertical"></i>
        </a>
      </li>
    </ul>
  </div>
  
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;">
			  <i class="layui-icon layui-icon-gift"></i>商品管理</a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;" data-url="goods/list.html">
				<i class="layui-icon layui-icon-cellphone-fine"></i>商品列表</a></dd>
            <dd><a href="javascript:;" data-url="goods/add.html">
			<i class="layui-icon layui-icon-addition"></i>添加商品</a></dd>            
          </dl>
        </li>
		
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;">
			  <i class="layui-icon layui-icon-template-1"></i>分类管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="sort/list.html">
				<i class="layui-icon layui-icon-cols"></i>分类列表</a></dd>
		    <dd><a href="javascript:;" data-url="sort/add.html">
			<i class="layui-icon layui-icon-addition"></i>添加分类</a></dd>		    		    
		  </dl>
		</li>
		
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;">
			  <i class="layui-icon layui-icon-cart-simple"></i>订单管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="">
				<i class="layui-icon layui-icon-note" ></i>订单列表</a></dd>	       
		  </dl>
		</li>
		
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;">
			  <i class="layui-icon layui-icon-group"></i>用户管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="/list.html">
				<i class="layui-icon layui-icon-friends"></i>用户列表</a></dd>		    
		  </dl>
		</li>
        
        
      </ul>
    </div>
  </div>
  
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;">everbody 一起嗨</div>
  </div>
  
  <div class="layui-footer">
    <!-- 底部固定区域 -->
    底部固定区域
  </div>
</div>
<script src="./layui/layui.js"></script>
<script src="js/cookie.js"></script>
<script>
//JS 
layui.use(['element', 'layer', 'util'], function(){
  var element = layui.element
  ,layer = layui.layer
  ,util = layui.util;
  $ = layui.$;
  
  //退出操作
  $("logout").click(function(){
	  //清除cookie 里de token
	  removeCookie('token');
	  //跳转到登录页面
	  window.location.href="login.html";
	  
  });
  
  $(".layui-nav-tree dd>a").click(function(){
	  var url = $(this).attr("data-url");
	  gotoUrl(url);
	
  });
  
  
  //头部事件
  util.event('lay-header-event', {
    //左侧菜单事件
    menuLeft: function(othis){
      layer.msg('展开左侧菜单的操作', {icon: 0});
    }
    ,menuRight: function(){
      layer.open({
        type: 1
        ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
        ,area: ['260px', '100%']
        ,offset: 'rt' //右上角
        ,anim: 5
        ,shadeClose: true
      });
    }
  });
  // 重写ajax方法
	$.ajaxx = $.ajax;
	$.ajax = function(options){
		if (options.dataType == 'html') {
			$.ajaxx(options);
			return;
		}
		//传递token
		var token = getCookie('token');
		if (token != null) {
			if (options.headers == undefined) {
				options.headers = {};
			}
			options.headers.token = token;
		}
		
		
		
		//重写success的处理
		if (options.success == undefined) {
			options.success = function(data,status,xhr){
				
			}
		}
		
		var success = options.success;
		options.success = function(data,status,xhr){
			//统一判断用户登录状态
			if (data.status == 401){
				layer.alert(data.msg,function(){
					window.location.href="login.html";
				});
			}
			
			//获取服务器通过headers返回的token
			var token = xhr.getResponseHeader('token');
			if(token != null)setCookie('token',token);
			
			//在调用原来的成功回调
			success(data,status,xhr);
		}
		
		$.ajaxx(options);
	}
  


checkLogin();
/**
 * 判断用户是否登录
 */
function checkLogin(){
	var token = getCookie("token");
	if (token.length == 0) {
		//没有登录信息
		window.location.href='login.html';
	} else{
		//登录信息已过期
		$.ajax({
			url:'http://localhost:9000/api/roots/'+token,
			type:'get',
			dataType:'json',
			success:function(data){
				
				root = data.data;
				
				//显示当前用户的名字
				$(".rootName").html(root.realname);
				//用户头像
				if(root.head == null || root.head.length == 0){
					$(".layui-nav-img").attr('src','img/7.jpg');
				}else{
					$(".layui-nav-img").attr('src','http://localhost:9000/upload/'+root.head);
				}
			}
		});
	}
	
}
});

function gotoUrl(url){
	if (url == null) {
		alert("超链接未定义data-url")
		return;
	}
	//使用ajax请求url
	$.ajax({
		url:url,
		type:'get',
		data:{},
		dataType:'html',
		success:function(data){
			$(".layui-body>div").html(data)
		},
		error:function(){
			
		}
		
	});
}
</script>
</body>
</html>